package yh.plugins.workflow;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import com.beaut.project.dao.GDMapper;
import com.beaut.project.service.GDService;
import com.beaut.project.util.FlowUtility;
import com.beaut.project.util.SpringContextUtil;

public class FlowCallBackFactory {
	private static FlowCallBackFactory fcbFactory=null;
	private FlowCallBackFactory(){
		
	}
	public static FlowCallBackFactory getInstance(){
		if(fcbFactory==null)
			fcbFactory=new FlowCallBackFactory();
		return fcbFactory;
	}
	public String getBusinessId(Connection conn,String runIdStr,int flowId) throws SQLException{
		 Statement stmt= conn.createStatement();
		 String sql="select business_id from business_flow where flow_run_id="+runIdStr+" and business_module='"+FlowUtility.getFlowNameById(flowId)+"'";
		 ResultSet rs=stmt.executeQuery(sql);
		 String businessId=null;
		 if(rs.next()){
			 businessId= rs.getString(1);
		 }
		 rs.close();
		 rs=null;
		 stmt.close();
		 stmt=null;
		 return businessId;
	}
	public void dealCallBack(Connection conn,String runId,int flowId){
		//GdReusePlugin==690
		//GdSelfRequestPlugin=691
		//GdCustomerBorrowPlugin=694
		//GdSelfBorrowPlugin=698
		//GdPurchaseApplyPlugin=701
		//GdJGRequestPlugin=708
		//GdSupplierApplyPlugin=711
		String businessKey="";
		 try {
			 businessKey = this.getBusinessId(conn, runId, flowId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	      Map<String,Object> queryMap = new HashMap<String,Object>();
	     
		switch(flowId){
			case FlowUtility.GD_REUSE_SHENPI_ID :{
				GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
				queryMap.put("reuse_id", businessKey);
				//更新工单预算审批占用数
		    	  try{
		    		  gdMapper.reusePluginBack(queryMap);
		    	  }
		    	  catch(Exception e){
		    		  gdMapper.reusePluginForward(queryMap);
		    	  }
				break;
			}
			case FlowUtility.GD_SELFREQUEST_SHENPI_ID :{
				GDService gdService = (GDService) SpringContextUtil.getBean("gdService");
				queryMap.put("id", businessKey);
			    	//更新工单预算审批占用数
			    	  try{
			    		  gdService.selfRequestPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdService.selfRequestPluginForward(queryMap);
			    	  }
				break;
			}
			case FlowUtility.GD_BORROW_CUSTOMER_SHENPI_ID :{
				 GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
				 queryMap.put("borrow_id", businessKey);
			    	  try{
			    		//更新审批占用数
			    		  gdMapper.customerBorrowPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdMapper.customerBorrowPluginForward(queryMap);
			    	  }
			      }
				break;
			case FlowUtility.GD_BORROW_SELF_SHENPI_ID :{
				GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
			    queryMap.put("borrow_id", businessKey);
			    	  try{
			    		//更新审批占用数
			    		  gdMapper.selfBorrowPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdMapper.selfBorrowPluginForward(queryMap);
			    	  }
				break;
			}
			case FlowUtility.GD_PURCHASE_APPLY_SHENPI_ID :{
				 GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
			      queryMap.put("apply_id", businessKey);
			    	  try{
			    		//更新审批占用数
			    		  gdMapper.purchaseApplyPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdMapper.purchaseApplyPluginForward(queryMap);
			    	  }
				break;
			}
			case FlowUtility.GD_JG_REQUEST_SHENPI_ID :{
				 GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
			      queryMap.put("req_id", businessKey);
			    	  try{
			    		//更新审批占用数
			    		  gdMapper.jgRequestPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdMapper.jgRequestPluginForward(queryMap);
			    	  }
				break;
			}
			case FlowUtility.GD_SUPPLIER_APPLY_SHENPI_ID :{
				GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
			      queryMap.put("supplier_apply_id", businessKey);
			    	  try{
			    		//更新审批占用数
			    		  gdMapper.supplierApplyPluginBack(queryMap);
			    	  }
			    	  catch(Exception e){
			    		  gdMapper.supplierApplyPluginForward(queryMap);
			    	  }
				break;
			}
			default:
				break;
		}
	}
}
